Skip to content

C# 14: Support extension types.#21220

Draft
michaelnebel wants to merge 15 commits intogithub:mainfrom
michaelnebel:csharp14/extension
Draft

C# 14: Support extension types.#21220
michaelnebel wants to merge 15 commits intogithub:mainfrom
michaelnebel:csharp14/extension

Conversation

@michaelnebel
Copy link
Contributor

@michaelnebel michaelnebel commented Jan 26, 2026

In this PR we introduce support for extension types (blocks). The feature is explained in detail here.
The feature generalises extensions to operators and properties.

Here is a small example of an declaration and how it can be invoked.

public static class MyExtensions
{
    extension(string s)
    {
        public bool IsValid() { return s is not null; }
    }
}

public class A
{
    public void M()
    {
        var s = "Hello World";
        s.IsValid(); // Call to extension method.
        MyExtensions.IsValid(s) // Call to compiler generated static method.
    }
}

It turns out that Roslyn generates a static method corresponding to the extension method. To avoid extracting multiple methods with identical bodies (which would further complicate the QL implementation) we

  • Only extract the IsValid() method, which has the qualified name MyExtensions.extension(string).IsValid.
  • Add a parameter s to IsValid() corresponding to the receiver parameter s. This needs to be synthesised as we can't re-use the parameter from the extension declaration.
  • Replace invocations of MyExtensions.IsValid with MyExtensions.extensions(string).IsValid.

DCA looks good.

  • Performance appears un-affected.
  • Nice reduction in the number of missing call targets for a range of projects (and an increase in the number of alerts).

@github-actions github-actions bot added the C# label Jan 26, 2026
Comment on lines +695 to +699
catch
{
// If anything goes wrong, fall back to the unbound declaration.
declaration = unboundDeclaration;
}

Check notice

Code scanning / CodeQL

Generic catch clause Note

Generic catch clause.
@michaelnebel michaelnebel changed the title [WIP] C# 14: Support extension blocks. C# 14: Support extension types. Feb 5, 2026
@michaelnebel
Copy link
Contributor Author

@hvitved : The PR is ready for review. However, I haven't added any upgrade/downgrade scripts yet in case the review leads to a change in the DB scheme.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant